# encoding: utf-8
# @File  : page_cart.py
# @Author: wu shaofan
# @Date  :  2024/04/17
# @Desc :
from selenium.webdriver.common.by import By
from PO.base.base import Base


class PageCart(Base):
    # 获取购物车商品名称
    get_goods_name = By.XPATH, '//div[@class="column t-goods"]//a'
    # 删除商品按钮
    delete_goods_button = By.LINK_TEXT, '删除'
    # 弹框中的删除按钮
    del_button = By.ID, 'removeGoods'
    # 搜索框输入商品
    search_goods_input = By.ID, 'q'
    # 搜索按钮
    search_button = By.CSS_SELECTOR, 'form[id="sourch_form"] button'
    # 加入购物车按钮
    add_cart_button = By.LINK_TEXT, '加入购物车'
    # 全选框按钮
    select_all_button = By.CSS_SELECTOR, 'div[class="column cart-checkbox"] i'
    # 勾选指定商品
    select_goods_button = By.CSS_SELECTOR, 'div[class="column "] i'
    # 去结算按钮
    settle_accounts_button = By.LINK_TEXT, '去结算'
    # 订单备注信息
    order_remark_input = By.CSS_SELECTOR, '.user_note_txt.fl'
    # 支付密码
    pay_password_input = By.ID, 'pay_pwd'
    # 提交订单按钮
    submit_order_button = By.ID, 'submit_order'
    # 订单提交成功的Toast信息
    submit_order_success_toast = By.CSS_SELECTOR, 'div[class="layui-layer-content layui-layer-padding"]'
    # 点击订单详情
    click_order_detail = By.LINK_TEXT, '订单详情'
    # 获取订单号
    get_order_number = By.CSS_SELECTOR, 'p[class="ddn1"] span'
    # 获取订单信息
    get_consignee_info = By.CSS_SELECTOR, 'div[class="psbaowq"] span'
    # 取消订单
    cancel_order = By.LINK_TEXT, '取消订单'
    # 确定取消
    confirm_cancel = By.LINK_TEXT, '确定'
    cancel_order_success = By.CSS_SELECTOR, '.layui-layer-content.layui-layer-padding'
    # 已取消文本
    cancel_text = By.CLASS_NAME, 'ddn2'

    # 根据商品名称取删除购物车商品
    def page_get_cart_goods_name_del(self, goods_name):
        goods_list = self.base_get_text_list(self.get_goods_name)
        i = 0
        for item in goods_list:
            if item == goods_name:
                # self.base_find_elements_index(self.delete_goods_button, i).click()
                self.base_find_elements(self.delete_goods_button)[i].click()
            i += 1
        return goods_list

    # 点击确认删除按钮
    def page_click_del_button(self):
        self.base_click(self.del_button)

    # 搜索框输入商品
    def page_input_search_goods(self, goods_name):
        self.base_input(self.search_goods_input, goods_name)
        self.base_get_allure_image()

    # 点击搜索按钮
    def page_click_search_button(self):
        self.base_click(self.search_button)
        self.base_get_allure_image()

    # 点击加入购物车按钮
    def page_click_add_cart_button(self):
        self.base_click(self.add_cart_button)
        self.base_get_allure_image()

    # 判断全选框是否被勾选
    def page_is_select_all(self):
        # 获取全选框class的属性值
        param = self.select_all_button
        element = self.base_get_element_attribute(param, "class")
        if element == "checkall checkFull":
            return False
        else:
            return True
        # # class的属性值 class="checkall checkFull" 表示未勾选
        # if element == "checkall checkFull":
        #     # 如果未勾选，先点击全选，确保全部商品都勾选
        #     self.base_click(param)
        #     # 点击点击一次，确定所有商品都没勾选
        #     self.base_click(param)
        # else:
        #     # 如果已勾选，点击点击一次，确定所有商品都没勾选
        #     self.base_click(param)

    # 勾选指定商品
    def page_click_select_goods(self, goods_name):
        # 获取购物车商品列表
        goods_list = self.base_get_text_list(self.get_goods_name)
        i = 0
        # # 遍历商品列表，判断商品是否在列表中，如果在则勾选
        # for item in goods_list:
        #     # 判断商品是否在列表中
        #     if item == goods_name:
        #         print("111")
        #         # 获取勾选按钮class的属性值
        #         param = self.select_goods_button
        #         element = self.base_get_elements_attribute(param, i, "class")
        #         # class的属性值 = checkall checkItem 表示没有勾选
        #         if element == "checkall checkItem ":
        #             print("222")
        #             # 点击勾选按钮
        #             self.base_find_elements(self.select_goods_button)[i].click()
        #     i = i + 1

        # 确保结算的时候只有指定的商品被勾选
        for item in goods_list:
            param = self.select_goods_button
            element = self.base_get_elements_attribute(param, i, "class")
            if element == "checkall checkItem ":
                if item == goods_name:
                    self.base_find_elements(param)[i].click()
            else:
                if item != goods_name:
                    self.base_find_elements(param)[i].click()
            i = i + 1
        self.base_get_allure_image()

    # 点击去结算按钮
    def page_click_settle_accounts_button(self):
        self.base_click(self.settle_accounts_button)
        self.base_get_allure_image()

    # 输入订单备注信息
    def page_input_order_remarks(self, order_remarks):
        self.base_input(self.order_remark_input, order_remarks)
        self.base_get_allure_image()

    # 输入支付密码
    def page_input_pay_password(self, pay_password):
        self.base_input(self.pay_password_input, pay_password)
        self.base_get_allure_image()

    # 点击提交订单按钮
    def page_click_submit_order_button(self):
        self.base_click(self.submit_order_button)
        self.base_get_allure_image()

    # 获取提交订单成功Toast提示信息
    def page_get_submit_order_success_toast(self):
        return self.base_get_toast_text(self.submit_order_success_toast)

    # 点击订单详情
    def page_click_order_detail(self):
        self.base_click(self.click_order_detail)
        self.base_get_allure_image()

    # 获取订单号
    def page_get_order_number(self):
        return self.base_find_elements(self.get_order_number)[1].text

    # 获取收货人信息
    def page_get_consignee_info(self):
        return self.base_get_text_list(self.get_consignee_info)

    # 点击取消订单
    def page_click_cancel_order(self):
        self.base_click(self.cancel_order)
        self.base_get_allure_image()

    # 点击确定取消订单按钮
    def page_click_confirm_cancel_order(self):
        # 点击确定按钮
        self.base_click(self.confirm_cancel)
        self.base_get_allure_image()
        # 获取弹框操作成功文本
        text = self.base_get_text(self.cancel_order_success)
        self.base_get_allure_image()
        # 添加强制等待，增加脚本稳定性
        self.base_time_sleep(0.1)
        # 再次点击确定按钮
        self.base_click(self.confirm_cancel)
        self.base_get_allure_image()
        return text

    # 获取取消订单成功文本
    def page_get_cancel_order_success_text(self):
        return self.base_get_text(self.cancel_text)
